var totalPage;
var totalCnt;
var changePartData = [];
var deleteflag ;

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	search();
	setTimeout(hideLoading, 500);
})
function search() {
	clearTable("changePartTbody");
	seach=1;
	showLoading();
	$.ajax({
		type : 'post',
		url : 'searchReplacePartRecord',
		data : $("#changePartSearch").serialize(),
		dataType : 'json',
		
		success : function(data) {
			setTimeout(hideLoading, 500);
			totalPage = data && data.totalPage || 0;
			totalCnt = data && data.totalCnt || 0;
			changePartData = data && data.changePartList || [];
			document.getElementById("total").innerText = '共' + totalCnt + '条 / 共' + totalPage + '页';
			// 查询数据的数组长度
			var length = changePartData.length;
			// 当前页数
			var currentPage = $('#currentPage').val();
			// 每页显示的条数
			var rows = $('#rows').val();
			// 画面表格的body
			var tbody = $("#changePartTbody");
			if (length == 0) {
				showMessge("没有查询结果");
			} else {
				for(var i=0; i<length; i++){
					// 序号
					var rowNo = (currentPage - 1) * rows + i + 1;
					// 追加显示数据
					var $tr = $('<tr>').appendTo(tbody);
					$("<td>").html(rowNo).appendTo($tr);
					$("<td>").html(changePartData[i].partName).appendTo($tr);
					$("<td>").html(changePartData[i].specification).appendTo($tr);
					$("<td>").html(changePartData[i].count).appendTo($tr);
				}
			}
		},
		error : function(data) {
			setTimeout(hideLoading, 500);
			if (data.status == "403" && data.statusText == "Forbidden") {
				showMessge("您没有操作权限!");
			} else {
				showMessge("查询失败!");
			}
		},
		timeout: function(data){
			setTimeout(hideLoading, 500);
			showMessge("操作超时！");
		}
	});
}
function save(){
	$('#saveTitle').html('');
	var saveTitle = $('#saveTitle');
	var strong = document.createElement("strong");
	strong.innerHTML = '更换零件记录添加';
    $("#save").modal("show");
    deleteflag = "save";
    saveTitle.append(strong);
	formReset("saveForm");
}
function confirm(){
	var savePartName = $("#savePartName").val();
	var saveSpecification = $("#saveSpecification").val();
	var saveCount = $("#saveCount").val();
	if (!savePartName) {
		showMessge("名称不能为空！");
		return;
	}
	if (!saveSpecification) {
		showMessge("规格型号不能为空！");
		return;
	}
	if (!saveCount) {
		showMessge("数量不能为空！");
		return;
	}
	if(isNaN(saveCount)){
		showMessge("请输入正确数值！");
		return;
	}
	$('#save').modal('hide');
		if(deleteflag == "save"){
			showLoading();
			$.ajax({
				type: "POST",
				url: 'saveReplacePartRecord',
		    	data: {
		    		PartName : savePartName,
		    		Specification : saveSpecification,
		    		Count : saveCount,
		    	},
		    	dataType : 'json',
				success: function(data){
					setTimeout(hideLoading, 500);
					$('#save').modal('hide');
					var result = data && data.result;
					if (result == "Success") {
							showMessge("添加成功");
					} else if (result == "NotFound") {
						showMessge("添加失败");
					} else {
						showMessge("添加异常");
					}
					search();
					formReset("saveForm");
				},
				error : function(data) {
				setTimeout(hideLoading, 500);
					if (data.status == "403" && data.statusText == "Forbidden") {
						showMessge("您没有操作权限!");
					} else {
						showMessge("添加失败!");
					}
				},
				timeout: function(data){
					setTimeout(hideLoading, 500);
					showMessge("操作超时！");
				}
			});
		}
	}
